package bimvu.yolaguidebook;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class SiteActivity extends FragmentActivity {

    private Site site;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            Object object = bundle.get(Site.class.toString());
            try {
                site = (Site) object;
                ApplicationData.getInstance().setActiveSite(site);
                updateView();
            } catch (NullPointerException e) {
            } catch (ClassCastException e) {
            }
        } else {
            Site activeSite = ApplicationData.getInstance().getActiveSite();
            if (activeSite != null) {
                site = activeSite;
                updateView();
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(Site.class.toString(), site);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Object object = savedInstanceState.getSerializable(Site.class.toString());
        try {
            site = (Site) object;
            updateView();
        } catch (NullPointerException e) {
        } catch (ClassCastException e) {
        }
    }

    @Override
    protected void onResume() {
        if (site != null) {
            updateView();
        }
        super.onResume();
    }

    private void updateView() {
        ((TextView) this.findViewById(R.id.siteTitle)).setText(site.title);
        setTitle(site.title);
        ((TextView) this.findViewById(R.id.siteDescription)).setText(site.description);

        Resources res = getResources();
        int mainPhotoId = res.getIdentifier(site.mainPhotoKey, "drawable", this.getPackageName());
        ImageView thumbView = (ImageView) findViewById(R.id.mainPhoto);
        thumbView.setImageResource(mainPhotoId);
    }

    @Override
    public boolean onNavigateUpFromChild(Activity child) {
        if (site != null) {
            updateView();
        }
        return super.onNavigateUpFromChild(child);
    }

    @Override
    protected void onRestart() {
        if (site != null) {
            updateView();
        }
        super.onRestart();
    }

    public void goToMap(View target) {
        Intent intent = new Intent(this, MapsActivity.class);
        intent.putExtra(Site.class.toString(), site);
        startActivity(intent);
    }

}
